package kitread.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import kitread.model.FeedItem;

import static org.apache.commons.lang3.StringEscapeUtils.escapeHtml4;

public class HTMLGenerator {

	public static File generateHTML(FeedItem i) {
		File tmpFile = null;
		try {
			tmpFile = File.createTempFile("" + i.hashCode(),".html");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		String page = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
				"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" +
				"<html><body style=\"margin: 0 20px; padding: 0; font-family: Cambria sans-serif;\">";
		page += "<div style='font-size: 12px; color: #9fb0b9; line-height: 1.8em;'>";
		for(String s : i.getPageContent().split("\n")) {
			page += "<p style=\"margin: 0 0 10px 0;\">" + escapeHtml4(s) + "</p>";
		}
		page += "</div>";
		page += "</body></html>";
		
		try {
			FileWriter fw = new FileWriter(tmpFile);
			fw.write(page);
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return tmpFile;
	}
}
